package com.zr.daoimpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.UpdateUserDao;
import com.zr.model.Clog;
import com.zr.model.Crole;
import com.zr.model.Cuser;

@Repository("updateUserDao")
public class UpdateUserDaoImpl<T> extends BaseDaoImpl<T> implements UpdateUserDao<T>{

	@Override
	public Crole getRoleByRname(String rname) {
		String hql = "from Crole where rname=:rname";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setString("rname", rname);
		List<Crole> rs = new ArrayList<Crole>();
		rs = que.list();
		return rs.get(0);
	}

	@Override
	public void updateUserByInfo(int uid, String ustate,Crole crole) {
		String hql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		List<Cuser> us = new ArrayList<Cuser>();
		us = que.list();
		Cuser u = us.get(0);
		u.setUstate(ustate);
		u.setCrole(crole);
		this.getCurrentSession().save(u);
	}

	@Override
	public void insertInfoIntoLog(String time, String uname, int uid, String info) {
		Clog clog = new Clog();
		clog.setLinfo(info);
		clog.setLtime(time);
		clog.setUname(uname);
		clog.setUid(uid);
		this.getCurrentSession().save(clog);
		
	}

	@Override
	public Cuser getUserByUid(int uid) {
		String hql = "from Cuser where uid=:uid";
		Query que = this.getCurrentSession().createQuery(hql);
		que.setInteger("uid", uid);
		List<Cuser> us = new ArrayList<Cuser>();
		us = que.list();
		Cuser u = us.get(0);
		return u;
	}

	

}
